<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 放置插槽公共注册好的组件 -->
      <PageTools :show-info="true">
        <template>
          <span slot="before">共{{ page.total }}条记录</span>
          <el-button slot="after" type="success" size="small">excel导入</el-button>
          <el-button slot="after" type="danger" size="small">excel导出</el-button>
          <el-button slot="after" type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </PageTools>
      <!-- 表格 -->
      <el-card>
        <el-table v-loading="loading" border :data="list">
          <el-table-column type="index" label="序号" sortable />
          <el-table-column prop="username" label="姓名" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式" sortable :formatter="formatEmployment" />
          <el-table-column prop="departmentName" label="部门" sortable />
          <el-table-column label="入职时间" sortable>
            <template v-slot="{row}">
              <!-- {{ row.timeOfEntry }} -->
              {{ row.timeOfEntry|formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" sortable>
            <template v-slot="{row}">
              <el-switch :value="row.enableState === 1" active-color="#13ce66" inactive-color="#ff4949" />
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable fixed="right" width="280">
            <template v-slot="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="delEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px;">
          <el-pagination layout="prev, pager, next" :total="page.total" :page-size="page.size" :current-page="page.page" @current-change="changePage" />
        </el-row>
      </el-card>
    </div>
    <!-- 新增员工弹出层 -->
    <!-- sync修饰符子组件改变父组件数据的语法糖 -->
    <addEmployee :show-dialog1.sync="showDialog" />
  </div>
</template>

<script>
import PageTools from '@/components/PageTools'
import { getEmployeeList, delEmployee } from '@/api/employees'
import EmployeeEnum from '@/api/constant/employees'
import addEmployee from './components/add-employee.vue'
export default {
  components: {
    PageTools,
    addEmployee
  },
  data() {
    return {
      loading: false,
      // 存放员工列表数据
      list: [],
      // 存放分页相关数据
      page: {
        page: 1, // 页码
        size: 10, // 每页条数
        total: 0
      },
      showDialog: false// 默认是关闭的弹层
    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    async getEmployeeList() {
      this.loading = true
      const { total, rows } = await getEmployeeList(this.page)
      console.log(total, rows)
      this.page.total = total
      this.list = rows
      this.loading = false
    },
    changePage(newPage) {
      console.log(newPage)
      this.page.page = newPage
      this.getEmployeeList()
    },
    formatEmployment(row, column, cellValue, index) {
      // console.log(row, column, cellValue, index)
      const obj = EmployeeEnum.hireType.find(item => item.id === cellValue)
      return obj ? obj.value : '未知'
    },
    async delEmployee(id) {
      try {
        await this.$confirm('确认删除该员工吗？')
        await delEmployee(id)
        this.getEmployeeList()
        this.$message.success('删除员工成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

  <style>

  </style>

